package com.qipay.sp.excutor.wanjia;

import com.alibaba.fastjson.JSONObject;
import com.qipay.baas.model.Order;
import com.qipay.order.config.OrderStateEnum;
import com.qipay.order.service.OrderService;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class WjAliWapExcutor extends AbstractExcutor {

    private final static String API_URL="api_url";
    @Autowired
    OrderService orderService;

    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.WX_NATIVE;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("price", new BigDecimal(order.getAmount()).divide(new BigDecimal("100"),2,BigDecimal.ROUND_DOWN));
        apiParams.put("istype",1);//1：支付宝；2：微信支付
        apiParams.put("orderid", order.getTradeNo());
        apiParams.put("orderuid",order.getCpId());//30000:微信扫码
        apiParams.put("goodsname",order.getBody());

        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {
        Object apiUrl = getVal(spconfig, API_URL);
        Object uid = getVal(spconfig, "uid");//商户uid
        Object notifyUrl= getVal(spconfig, "notify_url");
        Object returnUrl= getVal(spconfig, "return_url");
        Object token= getVal(spconfig, "token");

        params.put("uid",uid);
        params.put("notify_url",notifyUrl);
        params.put("return_url",returnUrl);
        String keyStr =generateStr(params,token.toString());

        String key = MD5Util.sign(keyStr,"utf-8").toLowerCase();
        params.put("key",key);

        String result = sendPostByKeyValue(params,apiUrl.toString());

        if (StringUtils.isBlank(result)) {
            throw new Exception("请求万家的微信接口失败");
        }
        Order order = orderService.getByTradeNo(params.get("orderid").toString());
//        order.setRemark(result);

        Map<String, Object> resultMap = new HashMap<>();
        JSONObject jsonObject = JSONObject.parseObject(result);
        if("1".equals(jsonObject.getString("code"))){
            Map<String, Object> responseMap = JSONObject.parseObject(jsonObject.getString("data"));
            resultMap.put("result_code", "0");
            resultMap.put("redirect_url", responseMap.get("qrcode"));
            order.setState(OrderStateEnum.WAITING.getType());
        }else{
            resultMap.put("result_code", "1");
            resultMap.put("message", jsonObject.getString("msg"));
            order.setState(OrderStateEnum.REQERROR.getType());
        }
        orderService.update(order);
        return resultMap;
    }

    public String generateStr(Map<String, Object>params ,String token){
        return params.get("goodsname").toString()+params.get("istype").toString()+params.get("notify_url").toString()
                +params.get("orderid").toString()+params.get("orderuid").toString()+params.get("price").toString()
                +params.get("return_url").toString()+token+params.get("uid").toString();
    }
}
